package com.example.demo.controller;

import com.example.demo.model.User;
import com.example.demo.service.CheckInService;
import com.example.demo.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/usercenter/checkin")
public class CheckInController {
    private final CheckInService checkInService;

    @Autowired
    public CheckInController(CheckInService checkInService) {
        this.checkInService = checkInService;
    }

    @PostMapping
    public Response<Map<String, Object>> checkIn() {
        Long userId = getCurrentUserId();
        Map<String, Object> result = checkInService.checkIn(userId);
        return Response.success(result);
    }

    @GetMapping("/status")
    public Response<Map<String, Object>> getCheckInStatus() {
        Long userId = getCurrentUserId();
        Map<String, Object> status = checkInService.getCheckInStatus(userId);
        return Response.success(status);
    }

    private Long getCurrentUserId() {
        return ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
    }
}